//
//  PresentedNavigationController.m
//  mmbang
//
//  Created by 肖信波 on 13-6-5.
//  Copyright (c) 2013年 iyaya. All rights reserved.
//

#import "PresentedNavigationController.h"
#import "Masonry.h"

@interface PresentedNavigationController ()

@property (nonatomic, copy) PresentAnimationFinishBlock presentFinishBlock;
@property (nonatomic, copy) LoginCompletionBlock loginCompletionBlock;

@end

@implementation PresentedNavigationController

#pragma mark - Life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarHeightChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}


- (void)statusBarHeightChanged:(NSNotification *)notification {
    [self resetViewFrame];
}

#pragma mark - Private 开启热点不下移20像素的问题，需要刷新
- (void)resetViewFrame {
    [self.view mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.offset([UIApplication sharedApplication].statusBarFrame.size.height > 20 ? 20 : 0);
        make.leading.offset(0);
        make.trailing.offset(0);
        make.bottom.offset(0);
    }];
}

#pragma mark - Present & Dismiss
- (void)present {
    [self presentWithCompletion:nil loginFinishedBlock:nil];
}

- (void)presentWithCompletion:(PresentAnimationFinishBlock)presentCompletion
           loginFinishedBlock:(LoginCompletionBlock)loginCompletion {
    self.presentFinishBlock = presentCompletion;
    self.loginCompletionBlock = loginCompletion;
    if (APP_DELEGATE.window == self.view.superview) {
        if (presentCompletion) {
            presentCompletion(self);
        }
        return;
    }
    CGRect loginFrame = self.view.frame;
    loginFrame.origin.y = APP_DELEGATE.window.frame.size.height;
    self.view.frame = loginFrame;
    [APP_DELEGATE.window addSubview:self.view];
    
    [self.rootViewController viewWillDisappear:YES];
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect loginFrame = self.view.frame;
        loginFrame.origin.y = 0.0f;
        self.view.frame = loginFrame;
    } completion:^(BOOL finished) {
        
        if (presentCompletion) {
            presentCompletion(self);
        }
        
    }];
}

- (void)presentWithNoAnimation {
    if (APP_DELEGATE.window == self.view.superview) {
        return;
    }
    CGRect loginFrame = self.view.frame;
    loginFrame.origin.y = 0;
    self.view.frame = loginFrame;
    [APP_DELEGATE.window addSubview:self.view];
    [self.rootViewController viewWillDisappear:YES];
}

- (void)dismiss {
    [self dismissWithCompletion:nil];
}

- (void)dismissWithCompletion: (void (^)(void))completion {
    [self.rootViewController viewWillAppear:YES];
    [UIView animateWithDuration:0.3 animations:^{
        CGRect loginFrame = self.view.frame;
        loginFrame.origin.y = APP_DELEGATE.window.frame.size.height;
        self.view.frame = loginFrame;
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self.rootViewController viewDidAppear:YES];
        if (completion) {
            completion();
        }
        if (self.loginCompletionBlock) {
            self.loginCompletionBlock(self, YES);
        }
    }];
}

@end
